package cn.javaxc.mvcwork.control.web;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.*;

import cn.javaxc.mvcwork.controller.web.PagedListInfo;
import cn.javaxc.model.base.*;
import cn.javaxc.mvcwork.util.common.CommonUtil;
import cn.javaxc.mvcwork.util.common.MapListUtil;
import cn.javaxc.mvcwork.util.common.StringUtils;
import cn.javaxc.mvcwork.util.helper.URLHelper;
import cn.javaxc.mvcwork.controller.*;
import cn.javaxc.mvcwork.controller.SystemStatus;
import cn.javaxc.mvcwork.framework.database.ConnectionElement;
import cn.javaxc.mvcwork.modules.login.UserBean;
import cn.javaxc.mvcwork.util.helper.URLHelper;
import cn.javaxc.mvcwork.util.common.MSecurityUtils;

public class WebUtils extends URLHelper{ 

	/**
	 * <font color='red' size='4' style='font-weight:900'>*</font>构建下拉框
	 * @param  name  Html的字段名
	 * @param  values  一列数据,形如:XXX1=1,XXX2=2,XXX3=3
	 * @param  _def  默认的选中值
	 */
	public static HtmlSelect getHtmlSelectString(String name,String values,Object _def){
		HtmlSelect sel = new HtmlSelect();
		for (String s:values.split(",") ){
			String[] option=s.split("=");
			if (option.length==2){
				sel.addItem(option[1].trim(),option[0].trim());
			}else if (s.endsWith("=")){
				sel.addItem("",option[0].trim());
			}else{			
				sel.addItem(s.trim());
			}
		}
		sel.setSelectName(name);
		if(_def!=null)sel.setDefaultValue(_def+"");
		return sel;
	}
	public static Object getValueName(String values,Object _def){
		for (String s:values.split(",") ){
			String[] option=s.split("=");
			if (option.length==2){
				if (option[1].trim().equals(_def+"")){
					return option[0].trim();
				}
			}
		}
		return _def;
	}
	public static String getSelectLink(String values,String link,String linkKey,Object appendurl,Object select) {
		return URLHelper.getSelectLink(getHtmlSelectString(linkKey,values,select),link,linkKey,appendurl,select);
	}

	/**
	 * <font color='red' size='4' style='font-weight:900'>*</font>构建JS的下拉框
	 * @param  name  Html的字段名
	 * @param  values  一列数据,形如:XXX1=1,XXX2=2,XXX3=3
	 * @param  _def  默认的选中值
	 * @param  jsstr  JS字串
	 */
	public static HtmlSelect getJSSelect(String name,String values,Object _def,String jsstr){
		HtmlSelect sel = getHtmlSelectString(name,values,_def);
		sel.setJsEvent(jsstr);
		return sel;
	}
	/**
	 * <font color='red' size='4' style='font-weight:900'>*</font>构建单选框
	 * @param  name  Html的字段名
	 * @param  values  一列数据,形如:XXX1=1,XXX2=2,XXX3=3
	 * @param  _def  默认的选中值
	 */
	public static HtmlSelect getHtmlRadioString(String name,String values,Object _def){
		HtmlSelect sel = new HtmlSelect();
		for (String s:values.split(",") ){
			String[] option=s.split("=");
			if (option.length==2){
				sel.addItem(option[1].trim(),option[0].trim());
			}else{
				sel.addItem(s.trim());
			}
			
		}
		sel.setSelectName(name);
		sel.setCheckboxOutput(true);
		if(_def!=null)sel.setDefaultValue(_def+"");
		return sel;
	}
	public static HtmlSelect getHtmlRadioString(String name,String values,Object _def,String jsstr){
		HtmlSelect sel = getHtmlRadioString(name,values,_def);
		sel.setJsEvent(jsstr);
		return sel;
	}
	public static HtmlSelect getHtmlCheckString(String name,String values,Object _def,String jsstr){
		HtmlSelect sel = getHtmlRadioString(name,values,_def);
		sel.setJsEvent(jsstr);
		sel.setCheckboxOutput(true);
		sel.setMultiple(true);
		if (_def!=null){
			sel.setMultipleSelects(CommonUtil.asList((_def+"").split(",")));
		}
		
		return sel;
	}
	public static SQLMap fromCode(SQLList list,Object value) {
		return list.fromCode(value+"");
	}
	public static String getDescByCode(SQLList list,Object value) {
		return list.getDescByCode(value);
	}
	public static String toHtml(Object s) {
		return s==null?"":StringUtils.toHtml(String.valueOf(s));
	}
	public static String showMessage(HttpServletRequest request, String errorEntry,String _def) {
		if (request.getAttribute(errorEntry)!=null){
			return "<font color='red'>" + toHtml((String)request.getAttribute(errorEntry)) + "</font>";	
		}
		return "<font color='#006699'>" + _def + "</font>";
    }
	public static String concat(Object s,Object s1){
		return ""+s+s1;
	}
	public static String concat2(Object s,Object s1,Object s2){
		return ""+s+s1+s2;
	}
	public static String concat3(Object s,Object s1,Object s2,Object s3){
		return ""+s+s1+s2+s3;
	}
	public static String concat4(Object s,Object s1,Object s2,Object s3,Object s4){
		return ""+s+s1+s2+s3+s4;
	}
	public static String concat5(Object s,Object s1,Object s2,Object s3,Object s4,Object s5){
		return ""+s+s1+s2+s3+s4+s5;
	}
	public static String concat6(Object s,Object s1,Object s2,Object s3,Object s4,Object s5,Object s6){
		return ""+s+s1+s2+s3+s4+s5+s6;
	}
	public static List toList(Object s,Object s1){
		return CommonUtil.asList(s,s1);
	}
	public static List toList2(Object s,Object s1,Object s2){
		return CommonUtil.asList(s,s1,s2);
	}
	public static List toList3(Object s,Object s1,Object s2,Object s3){
		return CommonUtil.asList(s,s1,s2,s3);
	}
	public static List toList4(Object s,Object s1,Object s2,Object s3,Object s4){
		return CommonUtil.asList(s,s1,s2,s3,s4);
	}
	public static List toList5(Object s,Object s1,Object s2,Object s3,Object s4,Object s5){
		return CommonUtil.asList(s,s1,s2,s3,s4,s5);
	}
	public static List toList6(Object s,Object s1,Object s2,Object s3,Object s4,Object s5,Object s6){
		return CommonUtil.asList(s,s1,s2,s3,s4,s5,s6);
	}
	public static Object getId(SQLMap emt,String key){
		return getPropertiesData(emt,key,"-1");
	}
	public static Object getIdData(SQLMap emt,String key){
		return getPropertiesData(emt,key,"-1");
	}

	public static Object getStringData(SQLMap emt,Object key){
		
		Object ret="";
		if (emt!=null&&key!=null){
			if (!"".equals(key)){
				Map m=emt;
				if (m.containsKey(key+"_desc")){
					ret=m.get(key+"_desc");
				}else{
					ret=m.get(key);
				}
				if (ret!=null&&ret instanceof java.sql.Timestamp){
					ret=ElementHelper.getDateString(emt,(String)key);
				}
			}
		}
		if (ret==null){
			return "";
		}
		return ret;
	}
	private static Object getPropertiesData(SQLMap emt,Object key,Object _def){
		Object ret=_def;
		if (emt!=null&&key!=null&&!"".equals(key)){
//			Map m=emt;
			if (!"".equals(key)){
					
//				if (m.get(key+"_idx")!=null){
//					ret=m.get(key+"_idx");
//				}else if (m.get("PrimaryId")!=null){
//					ret=m.get("PrimaryId");
//				}else{
					ret=emt.getMap(key);
//				}
			}
		}
		return ret;
	}
	public static String division(Integer i,Integer ii){
		try{
			return CommonUtil.format("%.2f", (double)i/ii);	
		}catch(Exception e){
		}
		return "NaN";
	}
	public static String toNameTime(double hour){
		try{
			if (hour<8){
				return CommonUtil.format("%.2f时",hour);
			}else{
				return CommonUtil.format("%.2f天",hour/8);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return "NaN";
	}
	public static Object isNull(Object value,Object _def){
		if (value==null){
			return _def;
		}else{
			return value;
		}
	}
	public static Object isEmpty(Object value,Object _def){
		if (StringUtils.isNullData(value)){
			return _def;
		}else{
			return value;
		}
	}
	public static int size(List list){
		return list.size();
	}

	public static Object getMultiQuoteText(Object bean,String column,String jnStr){
		if (bean instanceof SQLMap){
			SQLMap emt=(SQLMap)bean;
			return getStringData(emt,column);
		}if (bean instanceof SQLList){
			SQLList list=(SQLList)bean;
			List texts=MapListUtil.getDataList(list,column);
			if ("newline".equals(jnStr)){
				return CommonUtil.join(texts,"\n");
			}else{
				return CommonUtil.join(texts,jnStr);
			}
		}
		return "";
	}
	public static Object getMultiQuoteId(Object bean,String key,String jnStr){
		if (bean instanceof SQLMap){
			SQLMap emt=(SQLMap)bean;
			return getPropertiesData(emt,key,"-1");
		}if (bean instanceof SQLList){
			SQLList list=(SQLList)bean;
			if (list.size()>0){
				List ids=null;
				SQLMap emt=(SQLMap)list.get(0);
				Map m=emt;
//				if (m.get(key+"_idx")!=null){
//					ids=MapListUtil.getDataList(list,key+"_idx");
//				}else{
					ids=MapListUtil.getDataList(list,key);
//				}
				return CommonUtil.join(ids,jnStr);
			}
		}
		return "";
	}
	public static String getSortIcon(PagedListInfo page,String columnName) {
		return page.getSortIcon(columnName);
	}
	public static String getConfig(String key,String _def) {
		return MVCConfig.getConfig().get(key,_def);
	}
	public static String getChartjs(PageContext pageContext,SQLList list,String chart,String title) throws Exception{
		pageContext.getRequest().setAttribute("mvcchart",list);
		pageContext.getRequest().setAttribute("mvcchartattr",chart);
		pageContext.getRequest().setAttribute("mvccharttitle",title);
		pageContext.include(MVCConstant.JSPCTX+"/talent/page/chart.jsp" , true);
		return "";
	}
	public static Object loadClass(String clazz) throws Exception{
		return Class.forName(clazz).newInstance();
	}
}
